﻿using MXComponent.Common.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MXComponent.Api
{
    public class PLCClass
    {
        static string _intPath = AppDomain.CurrentDomain.BaseDirectory + "Setting.ini";
        static IniHelp inifile = new IniHelp(_intPath);
        public static int formulaStart;
        public static int formulaLength;
        public static int formulaFinish;
        public static int formulaPlanCount;
        public static int iLogicalStationNumber;

        public static int reportStart;
        public static int reportLength;

        public static int timerInterval;

        public static int statusStart;
        public static int statusLength;

        public static List<string> formulaAddressList = new List<string>();
        public static List<short> formulaValueList = new List<short>();

        public static List<string> reportAddressList = new List<string>();

        public static List<string> statusAddressList = new List<string>();

        public static void Init()
        {
            formulaStart = Convert.ToInt32(inifile.IniReadValue("Setting", "FormulaStart"));
            formulaLength = Convert.ToInt32(inifile.IniReadValue("Setting", "FormulaLength"));
            formulaFinish = Convert.ToInt32(inifile.IniReadValue("Setting", "FormulaFinish"));
            formulaPlanCount = Convert.ToInt32(inifile.IniReadValue("Setting", "FormulaPlanCount"));
            iLogicalStationNumber = Convert.ToInt32(inifile.IniReadValue("Setting", "LogicalStationNumber"));
            reportStart = Convert.ToInt32(inifile.IniReadValue("Setting", "ReportStart"));
            reportLength = Convert.ToInt32(inifile.IniReadValue("Setting", "ReportLength"));
            timerInterval = Convert.ToInt32(inifile.IniReadValue("Setting", "TimerInterval"));

            statusStart = Convert.ToInt32(inifile.IniReadValue("Setting", "StatusStart"));
            statusLength = Convert.ToInt32(inifile.IniReadValue("Setting", "StatusLength"));

            for (int i = 0; i < formulaLength; i++)
            {
                formulaAddressList.Add("D" + (formulaStart + i));
                formulaValueList.Add(0);
            }
            for (int i = 0; i < reportLength; i++)
            {
                reportAddressList.Add("D" + (reportStart + i));
            }
            for (int i = 0; i < statusLength; i++)
            {
                statusAddressList.Add("D" + (statusStart + i));
            }
        }

        public static void ResetList()
        {
            formulaAddressList.Clear();
            formulaValueList.Clear();
            for (int i = 0; i < formulaLength; i++)
            {
                formulaAddressList.Add("D" + (formulaStart + i));
                formulaValueList.Add(0);
            }
        }
        public static void DownFormula(List<short> paramList,int planCount)
        {
            for (int i = 0; i < formulaLength; i++)
            {
                if (i < paramList.Count)
                {
                    formulaValueList[i] = paramList[i];
                }
                else
                {
                    formulaValueList[i] = 0;
                }
            }
            formulaAddressList.Add("D"+ formulaFinish);
            formulaAddressList.Add("D" + formulaPlanCount);
            formulaValueList.Add(1);
            formulaValueList.Add(Convert.ToInt16(planCount));
        }
    }
}
